package com.jiangfeixiang.springbootblog.controller.admin;

import com.jiangfeixiang.springbootblog.entity.Users;
import com.jiangfeixiang.springbootblog.service.UsersService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 姜飞祥
 * @Description: 管理员登录
 * @Date: Create in 2019/1/20/0020 14:14
 * @param: $params$
 * @return: $returns$
 */
@Controller
@RequestMapping("/admin")
public class UsersLoginController {
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private UsersService usersService;


    /**
     * 跳转到登录页面
     * @return
     */
    @RequestMapping({"","/"})
    public String index(){

        return "admin/loginPage";
    }


    /**
     * 登录
     * @param users
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(Users users, HttpServletRequest request, Model model){
        Users user = usersService.login(users);
        if (user !=null){
            request.getSession().setAttribute("user",user);
            model.addAttribute("users",users);
            return "admin/admin-index";
        }
        return "admin/loginPage";
    }

    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping(value = "/loginOut",method = RequestMethod.GET)
    public String login(HttpServletRequest request){
        request.getSession().invalidate();
        return "admin/loginPage";
    }
}